/*
 *  Copyright (C) 2011 glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.swing.commons.listeners;

import java.util.ArrayList;
import java.util.List;
import org.balisunrise.common.Action;
import org.balisunrise.common.Event;

/**
 *
 * @author glauco
 */
public class FieldInputListener 
        implements org.balisunrise.common.listeners.FieldInputListener{

    private List<Action> gf;
    private List<Action> lf;
    private List<Action> ft;
    private List<Action> as;

    public FieldInputListener() {
        gf = new ArrayList<Action>();
        lf = new ArrayList<Action>();
        ft = new ArrayList<Action>();
        as = new ArrayList<Action>();
    }

    public void addGotFocusAction(Action action){
        if(gf.contains(action))
            return ;
        gf.add(action);
    }

    public void addLostFocusAction(Action action){
        if(lf.contains(action))
            return ;
        lf.add(action);
    }

    public void addFocusTraversalAction(Action action){
        if(ft.contains(action))
            return ;
        ft.add(action);
    }

    public void addAfterSyncAction(Action action){
        if(as.contains(action))
            return ;
        as.add(action);
    }

    @Override
    public void valueChanged(Event event) { }

    @Override
    public void gotFocus(Event event) {
        for (Action action : gf) {
            action.perform(event);
        }
    }

    @Override
    public void lostFocus(Event event) {
        for (Action action : lf) {
            action.perform(event);
        }
    }

    @Override
    public void focusTraversal(Event event) {
        for (Action action : ft) {
            action.perform(event);
        }
    }

    @Override
    public void afterSync(Event event) {
        for (Action action : as) {
            action.perform(event);
        }
    }

}
